
// pages/reportcard/reportcard.js
import https from '../../utils/Newrequests'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectMenu: {},
        switch: true,
        datalist: {},
        classInfo: {},
        singleDepartment: false,
        assignedPoints: true,
        menuList: [

        ],
        subjectAchievement: [],
        height: null,
        gradetype: null,
        gradeList: [],
        endowFlag: 1,
        pageNum: 1,      // 当前页
        pageSize: 30,    // 每页数量
        total: 0,
        loading: false,  // 是否正在加载
        noMore: false,   // 是否没有更多数据
    },
    navigateToNewSelectTranscripts() {
        // 获取当前 examId 参数
        const examId = this.data.examId;
        const selectTypeName = this.data.selectTypeName;
        // 跳转到目标页面并传递参数
        wx.navigateTo({
            url: `/pages/Newselecttranscript/Newselecttranscript?id=${examId}&selectTypeName=${selectTypeName}`,
        });
    },
    // 查看答题卡
    onNameClick(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/NewanswerSheet/NewanswerSheet?id=${id}&navId=${this.data.subjectId}&examId=${this.data.examId}&role=1`,
        })
    },
    changeMenu(e) {
        const selectedItem = e.detail;  // 子组件传上来的 {id, name, endowScore}
        const subjectId = selectedItem.id;
        const subjectName = selectedItem.name;
        const endowFlag = selectedItem.endowScore;
        this.setData({
            subjectId,
            subjectName,
            endowFlag
        });
        // 判断 id 是否为负数
        if (subjectId < 0) {
            this.setData({
                scoreType: 2,
                pageNum: 1,
                noMore: false,       // 重置“没有更多”状态
                loading: false,
            });
            // 如果 id 是负数，则不传 subjectId
            this.getGradeList(this.data.pageNum, this.data.pageSize, this.data.examId, this.data.classId, this.data.endowFlag,this.data.subjectGroup);

        } else {
            this.setData({
                scoreType: 1,
                pageNum: 1,
                noMore: false,       // 重置“没有更多”状态
                loading: false,
            });
            // 否则传递 subjectId
            this.getGradeList(this.data.pageNum, this.data.pageSize, this.data.examId, this.data.classId, this.data.endowFlag,this.data.subjectGroup,subjectId);
        }
        console.log("切换科目：", subjectId, subjectName, endowFlag);
    },
    //科目列表
    async getExamSubjectList(examId, subjectGroup, classId) {
        let options = {
            url: '/analysis/total/score/getExamSubjectList',
            method: 'get',
            data: { examId, subjectGroup, classId },
            header: { "content-type": "application/json;charset=UTF-8" },
        };

        try {
            // 发送请求
            let res = await https(options);

            // 如果请求成功，更新科目列表
            if (res.data.code === 200) {
                console.log(res.data.data, '考试概览数据');

                this.setData({
                    menuList: res.data.data || [],
                });
            } else {
                wx.showToast({
                    title: res.data.msg || '请求失败',
                    icon: 'none',
                });
            }
        } catch (error) {
            console.error('请求失败:', error);
            wx.showToast({
                title: '请求失败',
                icon: 'none',
            });
        }
    },
    //成绩单
    async getGradeList(pageNum, pageSize, examId, classId, endowFlag,subjectGroup,subjectId = null) {
        // 组装基础参数
        let data = { pageNum, pageSize, examId, classId, endowFlag,subjectGroup };

        // 只有 subjectId 有值时才加入
        if (subjectId !== undefined && subjectId !== null && subjectId !== '') {
            data.subjectId = subjectId;
        }
        let options = {
            url: '/analysis/score/studentScoreInfo',
            method: 'get',
            data,
            header: { "content-type": "application/json;charset=UTF-8" },
        };

        try {
            // 发送请求
            let res = await https(options);

            // 如果请求成功，更新科目列表
            if (res.data.code === 200) {
                console.log(res.data.rows, '班级成绩单数据');
                const list = res.data.rows
                const total = res.data.total || 0;
                this.setData({
                    gradeList: pageNum === 1 ? list : [...this.data.gradeList, ...list],
                    total,
                    noMore: pageNum * pageSize >= total,
                    loading: false,
                });
            } else {
                wx.showToast({
                    title: res.data.msg || '请求失败',
                    icon: 'none',
                });
                this.setData({ loading: false });
            }
        } catch (error) {
            console.error('请求失败:', error);
            wx.showToast({
                title: '请求失败',
                icon: 'none',
            });
            this.setData({ loading: false });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const { examId, subjectGroup, classId, selectTypeName, type } = options;
        console.log("Exam IDssss:", examId);
        console.log("Subject Group:", subjectGroup);
        console.log("classId", classId);
        console.log(wx.getStorageSync('classInfo'));

        this.setData({
            classInfo: wx.getStorageSync('classInfo'),
            examId,
            subjectGroup,
            selectTypeName,
            classId,
            gradetype: type
        });
        await this.getExamSubjectList(examId, subjectGroup, classId);
        await this.getGradeList(this.data.pageNum, this.data.pageSize, examId, classId, this.data.endowFlag, subjectGroup);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let query = wx.createSelectorQuery()
        query.select('#myElement').boundingClientRect((rect) => {
            //console.log(rect.top, 'ssaaaaaaaaaaaaaaaa') //距离顶部距离
            this.setData({
                height: rect.top + 10 + 'px'
            })

        }).exec()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.noMore) {
            wx.showToast({ title: '没有更多数据了', icon: 'none' });
            return;
        }
        const nextPage = this.data.pageNum + 1;
        this.setData({ pageNum: nextPage });
        this.getGradeList(nextPage, this.data.pageSize, this.data.examId, this.data.classId, this.data.endowFlag, this.data.subjectGroup,this.data.subjectId);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})